﻿package com.game.view.layer
{
	import AssetClass.*;
	
	import base.BaseHero;
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.data.vo.PetDataVO;
	import com.game.data.vo.SkillVO;
	import com.game.data.vo.attribute.RoleAttribVO;
	import com.game.event.GameEvent;
	import com.game.event.GameEventType;
	import com.game.manager.EventManager;
	import com.game.manager.NotificationManager;
	import com.game.module.backpack.vo.BackPackEvent;
	import com.game.view.component.icon.RoleSkillIcon;
	
	import export.pet.*;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class RoleInfo extends LayerBase
	{
		
		public function RoleInfo(tarHero:BaseHero,idx:int)
		{
			super();
			hero = tarHero;
			roleNum = idx;
			setHead();
		}
		
		public var hpTxt:TextField;
		public var mpTxt:TextField;
		public var expTxt:TextField;
		public var lvTxt:TextField;
		
		public var hpBar:MovieClip;
		public var mpBar:MovieClip;
		public var expBar:MovieClip;
		public var wsBar:MovieClip;
		public var wsShow:MovieClip;
		
		public var bgPanel:Sprite;
		public var roleHead:MovieClip;
		public var typeTxt:Sprite;
		public var skill1:MovieClip;
		public var skill2:MovieClip;
		public var skill3:MovieClip;
		public var skill4:MovieClip;
		public var skill5:MovieClip;
		
		public var btn_pack:SimpleButton;
		public var btn_set:SimpleButton;
		public var btn_skill:SimpleButton;
		public var btn_magic:SimpleButton;
		public var btn_pet:SimpleButton;
		
		internal var count:int;
		private var roleNum:int;
		private var petHead:ShowPetInfo;
		private var hero:BaseHero;
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("RoleInfo") as MovieClip;
			TextUtil.redrawTxt(mc);
			
			bgPanel = mc.getChildByName("bgPanel") as Sprite;
			roleHead = mc.getChildByName("roleHead") as MovieClip;
			roleHead.gotoAndStop(1);
			wsShow = mc.getChildByName("wsShow") as MovieClip;
			wsShow.gotoAndStop(1);
			wsBar = mc.getChildByName("wsBar") as MovieClip;
			wsBar.gotoAndStop(1);
			
			
			hpBar = mc.getChildByName("hpBar") as MovieClip;
			hpBar.gotoAndStop(1);
			mpBar = mc.getChildByName("mpBar") as MovieClip;
			mpBar.gotoAndStop(1);
			expBar = mc.getChildByName("expBar") as MovieClip;
			expBar.gotoAndStop(1);
			typeTxt = mc.getChildByName("typeTxt") as MovieClip;
			
			skill1 = mc.getChildByName("skill1") as MovieClip;
			skill2 = mc.getChildByName("skill2") as MovieClip;
			skill3 = mc.getChildByName("skill3") as MovieClip;
			skill4 = mc.getChildByName("skill4") as MovieClip;
			skill5 = mc.getChildByName("skill5") as MovieClip;
			
			lvTxt = mc.getChildByName("lvTxt") as TextField;
			hpTxt = mc.getChildByName("hpTxt") as TextField;
			mpTxt = mc.getChildByName("mpTxt") as TextField;
			expTxt = mc.getChildByName("expTxt") as TextField;
			
			btn_pack = mc.getChildByName("btn_pack") as SimpleButton;
			btn_pet = mc.getChildByName("btn_pet") as SimpleButton;
			btn_magic = mc.getChildByName("btn_magic") as SimpleButton;
			btn_set = mc.getChildByName("btn_set") as SimpleButton;
			btn_skill = mc.getChildByName("btn_skill") as SimpleButton;
			addChild(mc);
		}
		
		public function render(ratio:Number):void
		{
		
		}
		
		public function renderAnimate():void
		{
			if (!hero || !hero.roleProperties)
			{
				return;
			}
			var vo:RoleAttribVO = hero.getAttribute() as RoleAttribVO;
			hpTxt.text = vo.hp + "/" + vo.hpMax;
			hpBar.gotoAndStop(1 + 100 - vo.getHpPercent());
			mpTxt.text = vo.mp + "/" + vo.mpMax;
			mpBar.gotoAndStop(1 + 100 - vo.getMpPercent());
			expTxt.text = vo.exp + "/" + vo.needExp;
			expBar.gotoAndStop(1 + Math.round(100 * (1 - vo.exp / vo.needExp)));
			lvTxt.text = hero.roleProperties.level.toString();
			wsBar.gotoAndStop(Math.round(hero.roleProperties.energy));
			wsShow.gotoAndStop(hero.roleProperties.energy >= 100 ? 2 : 1);
			if(wsShow.currentFrame == 2)
			{
				var mc:MovieClip = wsShow.getChildAt(0) as MovieClip;
				if(mc.currentFrame == mc.totalFrames)
				{
					mc.gotoAndStop(1);
				}
				mc.nextFrame();
			}
		}
		
		private function setHead():void
		{
			roleHead.gotoAndStop(hero.getPlayer().getRoleName());
		}
		
		public function setSkillIcon():void
		{
			var idx:int;
			const mcName:String = "skillIcon";
			for(idx = 1; idx <= 5; idx++)
			{
				var icon:RoleSkillIcon = (this["skill" + idx] as MovieClip).getChildByName(mcName) as RoleSkillIcon;
				if(icon != null)
				{
					icon.removeFromParent();
				}
			}
			for(idx = 1; idx <= 5; idx++)
			{
				if(hero.getPlayer().getEquippedSkillByIndex(idx) == null)
				{
					continue;
				}
				var vo:SkillVO = hero.getPlayer().getEquippedSkillByIndex(idx);
				var stdVO:SkillVO = gc.configManager.skillProxy.getSkillById(vo.skillId);
				stdVO.skillLevel = vo.skillLevel;
				icon = new RoleSkillIcon();
				icon.name = mcName;
				icon.setVO(stdVO);
				icon.setSkillKey(gc.keyConfig.getSkillKeyByIndex(idx));
				icon.changeState(RoleSkillIcon.DISPLAY_SKILL_INFO);
				icon.width = (this["skill" + idx] as MovieClip).width - 2;
				icon.height = (this["skill" + idx] as MovieClip).height - 2;
				icon.x = icon.y = 1;
				(this["skill" + idx] as MovieClip).addChild(icon);
			}
		}
		
		override protected function onAdded():void
		{
			btn_pack.addEventListener(MouseEvent.CLICK, showBackPack);
			btn_set.addEventListener(MouseEvent.CLICK, setClick);
			btn_skill.addEventListener(MouseEvent.CLICK, studySkill);
			btn_magic.addEventListener(MouseEvent.CLICK, fbClick);
			btn_pet.addEventListener(MouseEvent.CLICK, cwClick);
			setSkillIcon();
			addPetHead();
		}
		
		override protected function onRemoved():void
		{
			btn_pack.removeEventListener(MouseEvent.CLICK, showBackPack);
			btn_set.removeEventListener(MouseEvent.CLICK, setClick);
			btn_skill.removeEventListener(MouseEvent.CLICK, studySkill);
			btn_magic.removeEventListener(MouseEvent.CLICK, fbClick);
			btn_pet.removeEventListener(MouseEvent.CLICK, cwClick);
			hero = null;
		}
		
		public function addPetHead():void
		{
			var vo:PetDataVO = hero.getPlayer().findCurrentPet();
			if (vo)
			{
				if (petHead == null)
				{
					petHead = new ShowPetInfo();
					petHead.setPetInfo(vo);
					petHead.flipHorizontalTxt(roleNum);
					petHead.y = 94;
					addChild(petHead);
				}
				else
				{
					petHead.setPetInfo(vo);
				}
			}
			else
			{
				removePetHead();
			}
		}
		
		private function removePetHead():void
		{
			if (Boolean(petHead) && contains(petHead))
			{
				removeChild(petHead);
				petHead = null;
			}
		}
		
		private function showBackPack(param1:MouseEvent):void
		{
			if (gc.curStage == 0 && gc.curLevel == 2 || gc.curStage == 16)
			{
				return;
			}
			if (hero.isDead())
			{
				NotificationManager.alert("您的角色已经死亡", 2);
				return;
			}
			if (!gc.isStopGame)
			{
				var inter:BackPackInter = new BackPackInter();
				inter.setPackOwner(hero);
				EventManager.getIns().dispatchEvent(GameEventType.PAUSE_GAME);
			}
			else
			{
				EventManager.getIns().dispatchEvent(BackPackEvent.CLOSE_BACKPACK);
			}
		}
		
		private function setClick(param1:MouseEvent):void
		{
			if (!gc.isStopGame)
			{
				parent.addChild(new SetMenu());
				EventManager.getIns().dispatchEvent(GameEventType.PAUSE_GAME);
			}
			else
			{
				EventManager.getIns().dispatchEvent(GameEventType.CLOSE_SET_MENU);
			}
		}
		
		private function studySkill(param1:MouseEvent):void
		{
			if (gc.curStage == 0 && gc.curLevel == 2)
			{
				return;
			}
			if (gc.isStopGame)
			{
				EventManager.getIns().dispatchEvent(GameEventType.CLOSE_MANAGE_SKILL);
			}
			else
			{
				new RoleSkillInter();
			}
		}
		
		private function fbClick(param1:MouseEvent):void
		{
			var _loc2_:* = null;
			if (gc.curStage == 0 && gc.curLevel == 2 || gc.curStage == 16)
			{
				return;
			}
			if (hero.isDead())
			{
				NotificationManager.alert("您的角色已经死亡", 2);
				return;
			}
			if (hero.getPlayer().getCurEquipByType("zbfb") != null)
			{
				_loc2_ = new SutraInter();
				_loc2_.setRole(hero);
			}
			else
			{
				NotificationManager.alert("您还未装备法宝", 2);
			}
		}
		
		private function cwClick(param1:MouseEvent):void
		{
			var _loc2_:* = null;
			if (gc.curStage == 0 && gc.curLevel == 2 || gc.curStage == 16)
			{
				return;
			}
			if (!gc.isStopGame)
			{
				_loc2_ = new RolePetInter();
				_loc2_.setRole(hero.getPlayer());
				EventManager.getIns().dispatchEvent(GameEventType.PAUSE_GAME);
			}
			else
			{
				EventManager.getIns().dispatchEvent(GameEventType.CLOSE_MANAGE_PET);
			}
		}
	}
}
